home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / space / software / unix / xanim / xanm_txt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-04  |  4.4 KB  |  177 lines

  1.  
  2. /*
  3.  * xanim_txt.c
  4.  *
  5.  * Copyright (C) 1990,1991,1992 by Mark Podlipec. 
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified and redistributed
  9.  * without fee provided that this copyright notice is preserved 
  10.  * intact on all copies and modified copies.
  11.  * 
  12.  * There is no warranty or other guarantee of fitness of this software.
  13.  * It is provided solely "as is". The author(s) disclaim(s) all
  14.  * responsibility and liability with respect to this software's usage
  15.  * or its effect upon hardware or computer systems.
  16.  *
  17.  */
  18.  
  19. #include "xanim.h"
  20. #include "xanim_gif.h"
  21.  
  22. int Is_TXT_File();
  23. void TXT_Read_File();
  24.  
  25. /*
  26.  * This file will open the filename passed to it and determine if
  27.  * it is a txt91 file. If it is it returns TRUE, else FALSE. It
  28.  * closes the file before returning.
  29.  */
  30. int Is_TXT_File(filename)
  31. char *filename;
  32. {
  33.  FILE *fp;
  34.  ULONG firstword;
  35.  
  36.  if ( (fp=fopen(filename,"r")) == 0)
  37.  { fprintf(stderr,"can't open %s\n",filename); TheEnd();}
  38.  /* by reading bytes we can ignore big/little endian problems */
  39.  firstword  = (fgetc(fp) & 0xff) << 24;
  40.  firstword |= (fgetc(fp) & 0xff) << 16;
  41.  firstword |= (fgetc(fp) & 0xff) <<  8;
  42.  firstword |= (fgetc(fp) & 0xff);
  43.  fclose(fp);
  44.  
  45. /*                   t x t 9     so we ignore the 1 */
  46.  if (firstword == 0x74787439) return(TRUE);
  47.  return(FALSE);
  48. }
  49.  
  50. static char gif_file_name[256];
  51.  
  52. /*
  53.  * This file parse the txt animation file and converts it into actions. 
  54.  *
  55.  */
  56. void TXT_Read_File(fname,anim_hdr)
  57. ANIM_HDR *anim_hdr;
  58. char *fname;
  59. {
  60.  FILE *fp;
  61.  int ret,i;
  62.  int num_of_files,txtframe_num;
  63.  int *txt_act_lst;
  64.  
  65.  if ( (fp=fopen(fname,"r"))==0)
  66.  { 
  67.   fprintf(stderr,"Can't open %s for reading.\n",fname); 
  68.   TheEnd();
  69.  }
  70.  
  71.  /* read and throw away txt91 header 
  72.   */
  73.  fscanf(fp,"%*s",gif_file_name);
  74.  
  75.  /* Read the number of files
  76.   */
  77.  fscanf(fp,"%ld",&num_of_files);
  78.  if (num_of_files<=0)
  79.  {
  80.   fprintf(stderr,"num_of_file is wierd=%ld\n",num_of_files);
  81.   fclose(fp);
  82.   TheEnd();
  83.  }
  84.  
  85.  txt_act_lst = (int *) malloc( sizeof(int) * (num_of_files + 1));
  86.  /* Read in the GIF files, use only the 1st one's colormap
  87.   */
  88.  for(i=0;i<num_of_files;i++)
  89.  {
  90.   fscanf(fp,"%s",gif_file_name);
  91.   fprintf(stderr,"Reading %s\n",gif_file_name);
  92.   txt_act_lst[i] = action_cnt;
  93.   GIF_Read_File(gif_file_name);
  94.  }
  95.  txt_act_lst[num_of_files] = action_cnt;
  96.  
  97.  /* Check for Frame list at end of images.
  98.   */
  99.  ret=fscanf(fp,"%ld",&txtframe_num);
  100.  if ( (ret==1) && (txtframe_num>=0))
  101.  {
  102.   int *txt_frames,tmp_txtframe,num_valid_txtframes;
  103.   int numof_frames,j,k;
  104.  
  105.   /* read in txt frame list, keep track of actual frames since each txt_frame
  106.    * can have several frames(cmaps and images);
  107.    */
  108.   txt_frames = (int *) malloc(txtframe_num * sizeof(int) );
  109.   numof_frames = 0;
  110.   num_valid_txtframes = 0;
  111.   for(i=0; i<txtframe_num; i++)
  112.   {
  113.    ret = fscanf(fp,"%ld",&tmp_txtframe);
  114.    if ( (ret==1) && (tmp_txtframe >= 0) && (tmp_txtframe < num_of_files) )
  115.    {
  116.     txt_frames[num_valid_txtframes] = tmp_txtframe;
  117.     numof_frames += txt_act_lst[ tmp_txtframe + 1 ] 
  118.                                - txt_act_lst[ tmp_txtframe ];
  119.     num_valid_txtframes++;
  120.    }
  121.    else
  122.    {
  123.     fprintf(stderr,"TXT_READ: bad frame number (%ld) in frame list\n",
  124.                             tmp_txtframe);
  125.    }
  126.   }
  127.   /* Allocate a frame_lst of that size.
  128.    */
  129.   anim_hdr->frame_lst = (int *)malloc(sizeof(int) * (numof_frames + 1));
  130.   if (anim_hdr->frame_lst == NULL)
  131.                    TheEnd1("TXT_ANIM: couldn't malloc for frame_lst\0");
  132.  
  133.   j = 0;
  134.   for(i=0; i<num_valid_txtframes; i++)
  135.   {
  136.    for(k=txt_act_lst[ txt_frames[i] ]; k < txt_act_lst[ txt_frames[i]+1 ]; k++)
  137.    {
  138.     anim_hdr->frame_lst[j] = k;
  139.     j++;
  140.    }
  141.   }
  142.  
  143.   free(txt_frames);
  144.   /* put into animfile header
  145.    */
  146.   anim_hdr->frame_lst[numof_frames] = -1;
  147.   anim_hdr->loop_frame = 0;
  148.   anim_hdr->last_frame = numof_frames - 1;
  149.  }
  150.  /* If there is no frame list then create a sequential one
  151.   */
  152.  else
  153.  {
  154.   int numof_actions;
  155.  
  156.   /* the number of actions is the number of frames 
  157.    */
  158.   numof_actions = action_cnt - action_start;
  159.   /* malloc frame array plus 1 for ending frame
  160.    */
  161.   anim_hdr->frame_lst = (int *)malloc(sizeof(int) * (numof_actions+1));
  162.   if (anim_hdr->frame_lst == NULL)
  163.                    TheEnd1("TXT_ANIM: couldn't malloc for frame_lst\0");
  164.   for(i=0; i < numof_actions; i++) anim_hdr->frame_lst[i]=action_start+i;
  165.  
  166.   /* put into animfile header
  167.    */
  168.   anim_hdr->frame_lst[numof_actions] = -1;
  169.   anim_hdr->loop_frame = 0;
  170.   anim_hdr->last_frame = numof_actions - 1;
  171.  }
  172.  
  173.  free(txt_act_lst);
  174.  fclose(fp);
  175. }
  176.  
  177.